/**
  * 系统管理--用户管理的单例对象
 */
var Subjects = {
    id: "SubjectsTable",//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1,
    deptid:0
};

/**
 * 初始化表格的列
 */
Subjects.initColumn = function () {
    var columns = [
        {field: 'selectItem', radio: true},
            {title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle'},
            {title: '检查表名称', field: 'subNo', visible: true, align: 'center', valign: 'middle'},
            {title: '检查表id', field: 'catagoryId', visible: false, align: 'center', valign: 'middle'},
            {title: '题目序号', field: 'sort', visible: true, align: 'center', valign: 'middle'},
            {title: '题目内容', field: 'content', visible: true, align: 'center', width:'250px', valign: 'middle'},
            {title: '总分值', field: 'helps', visible: true, align: 'center', valign: 'middle'},
            {title: '评分标准', field: 'remark', visible: true, align: 'center', valign: 'middle'}
            /*{title: '题目类别', field: 'examClass', visible: true, align: 'center', valign: 'middle',
            	formatter : function(value, row,index) {
            		if (value == "1") {
    					return '单选题';
    				} else if (value == "2") {
    					return '多选题';
    				}else if (value == "3") {
    					return '判断题';
    				}
            	}},*/
            
            /*{title: '题目图片', field: 'examImageUrl', visible: true, align: 'center', valign: 'middle'},*/
           /*{title: '正确答案', field: 'answer', visible: true, align: 'center', valign: 'middle'},*/
           
            /*{title: '帮助URL', field: 'helpsUrl', visible: false, align: 'center', valign: 'middle'},
            {title: '答题次数', field: 'times', visible: false, align: 'center', valign: 'middle'},*/
            /*{title: '创建时间', field: 'createTime', visible: false, align: 'center', valign: 'middle'},
            {title: '修改时间', field: 'updateTime', visible: false, align: 'center', valign: 'middle'},
           {title: '创建人', field: 'createUser', visible: false, align: 'center', valign: 'middle'},
            {title: '修改人', field: 'updateUser', visible: false, align: 'center', valign: 'middle'},*/
           
          /* {title: '选项', field: 'sound', visible: true, align: 'center', valign: 'middle'},*/
            /*{title: '语音讲解内容', field: 'voiceHelp', visible: true, align: 'center', valign: 'middle'}*/
        ];
    return columns;
};

/**
 * 检查是否选中
 */
Subjects.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    alert(selected.id);
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        Subjects.seItem = selected;
        return true;
    }
};

Subjects.singleCheck = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
    	if (selected.length >1) {
            Feng.info("请先选中表格中的某一记录！");
            return false;
        }else{
        	 Subjects.seItem = selected[0];
             return true;
        } 
    }
};

/**
 * 点击添加
 */
Subjects.openAddSubjects = function () {
    var index = layer.open({
        type: 2,
        title: '添加',
        area: ['100%', '100%'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/subjects/subjects_add'
    });
    this.layerIndex = index;
};

/**
 * 点击修改按钮时
 * @param userId 
 */
Subjects.openChange = function () {
    if (this.singleCheck()) {
        var index = layer.open({
            type: 2,
            title: '编辑',
            area: ['90%', '90%'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/subjects/toUpdate/' + this.seItem.id
        });
        this.layerIndex = index;
    }
};
Subjects.openView = function(){
	if (this.singleCheck()) {
        var index = layer.open({
            type: 2,
            title: '详情',
            area: ['100%', '100%'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/Subjects/view/' + this.seItem.id
        });
        this.layerIndex = index;
    }
}

Subjects.openDel = function () {
    if (this.singleCheck()) {
    	
    	var id=this.seItem.id;
        var operation = function(){
            var userId = Subjects.seItem.id;
            var ajax = new $ax(Feng.ctxPath + "/subjects/delete", function (data) {
            	if(data.resultCode==0){
            		Feng.success("删除成功!");
            		Subjects.table.refresh();
            	}else{
            		Feng.error(data.msg + "!");
            	}              
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", id);
            ajax.start();
        };

        Feng.confirm("是否删除选中的题目?",operation);
    }
};




Subjects.resetSearch = function () {
    $("#catagoryId").val("");
    $("#content").val("");
    Subjects.table.refreshOptions({pageNumber:1});
    Subjects.search();
}

Subjects.search = function () {
    var queryData = {};
    queryData['catagoryId'] = $("#catagoryId").val();
    queryData['content'] = $("#content").val();
    Subjects.table.refreshOptions({pageNumber:1});
    Subjects.table.refresh({query: queryData});
}

Subjects.openSubjectsOption = function () {
	
    if (this.singleCheck()) {
        var index = layer.open({
            type: 2,
            title: '题目答案选项',
            area: ['800px', '520px'], // 宽高
            fix: false, // 不固定
            maxmin: true,
            content: Feng.ctxPath + '/subjects/subjects_option/' + Subjects.seItem.id
        });
        this.layerIndex = index;
    }
};


$(function () {
	var paramsId =[
	      {
	    	  "key":"catagoryId",
	    	  "type":"select",
	    	  "id":"catagoryId"
	       },
	       {
		       "key":"content",
		       "type":"input",
		       "id":"content"
		   }
	   /*{
	       "key":"courseName",
	       "type":"input",
	       "id":"courseName"
	   },
	   {
		   "key":"termName",
		   "type":"select",
		   "id":"termName"
	   },
	   {
		   "key":"catagoryId",
		   "type":"select",
		   "id":"catagoryId"
	   }*/
	];
    var defaultColunms = Subjects.initColumn();
    var table = new BSTable("SubjectsTable", "/subjects/list", defaultColunms,paramsId);
    table.setPaginationType("server");
    Subjects.table = table.init();
});






